<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
loadcache('plugin');
if(!defined('Adz_alias_fun')) include('alias.fun.php');
if(in_array($_GET['mod'],array('rss')) || !$_GET['mod']){
	loadcache('plugin');
	$rule = $_G['setting']['rewriterule']['forum_viewthread'];
	$rl_trr = explode('{tid}',$rule);
	$aconfig = $_G['cache']['plugin']['alias'];
	$fm_trr = explode('{alias}',$aconfig['fm_tid']);
	$note = ob_get_contents();
	ob_end_clean();
	ob_start();
	if($_GET['mod']=='rss'){
		preg_match_all('/<link>(.*?)'.str_replace(array('{tid}','{page}','{prevpage}','.'),array('(\d+)','(\d+)','(\d+)','\.'),$rule).'/i',$note,$nrr);
	}else{
		preg_match_all('/href="(.*?)'.str_replace(array('{tid}','{page}','{prevpage}','.'),array('(\d+)','(\d+)','(\d+)','\.'),$rule).'/i',$note,$nrr);
	}
	$num = count($nrr[1]);
	for($i=0;$i<$num;$i++){
		if(!empty($nrr[2][$i])) $tids[$nrr[2][$i]] = 1;
	}
	if(count($tids)>0){
		$arr = Adz::fetch_con('plugin_alias_bind',"status=1 and type=1 and rid in (".implode(",",array_keys($tids)).")");
		foreach ($arr as $k => $v) {
			if($aconfig['urlcode']==1){
				if($_G['charset']=='gbk'){
					if(function_exists('mb_convert_encoding')){
						if($charsetto) $v['alias'] = mb_convert_encoding($v['alias'],'utf-8','GB2312');
					}else{
						if($charsetto) $v['alias'] = iconv('GB2312','utf-8',$v['alias']);
					}
				}elseif($_G['charset']=='big5'){
					if(function_exists('mb_convert_encoding')){
						$v['alias'] = mb_convert_encoding($v['alias'],'utf-8','BIG5');
					}else{
						$v['alias'] = iconv('BIG5','utf-8',$v['alias']);
					}
				}
				$v['alias'] = rawurlencode($v['alias']);
			}
			$old[] = str_replace(array('{tid}','{page}','{prevpage}'),array($v['tid'],1,1),$rule);
			$rep[] = $fm_trr[0].$v['alias'].$fm_trr[1];
			$old[] = $rl_trr[0].$v['tid'].'-';
			$rep[] = $fm_trr[0].$v['alias'].'-';
		}
		$note = str_replace($old,$rep,$note);
	}
	echo $note;
}
?>